<template>
  <view class="todo-list">
    <view v-if="todos.length === 0" class="empty-list">
      <text class="empty-text">回收站为空</text>
    </view>

    <view v-else class="todo-items">
      <view v-for="(item, index) in todos" :key="item.id" class="todo-item">
        <!-- 左侧CheckBox按钮 -->
        <view class="checkbox" :class="{ completed: item.completed }">
          <image
            v-if="item.completed"
            class="check-icon"
            src="/static/svgs/confirm.svg"
          ></image>
        </view>

        <!-- 中间内容 -->
        <view class="todo-content">
          <text
            class="todo-text"
            :class="{ 'completed-text': item.completed }"
            >{{ item.content }}</text
          >
          <text v-if="item.dueDate" class="todo-date">{{
            formatDate(item.dueDate)
          }}</text>
        </view>

        <!-- 右侧按钮组 -->
        <view class="action-buttons">
          <!-- 恢复按钮 -->
          <view class="action-btn restore-btn" @click="restoreTodo(item.id)">
            <image class="action-icon" src="/static/svgs/restore.svg"></image>
          </view>
          <!-- 删除按钮 -->
          <view class="action-btn delete-btn" @click="deleteTodo(item.id)">
            <image class="action-icon" src="/static/svgs/delete.svg"></image>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref } from "vue";

// 定义组件属性
const props = defineProps({
  todos: {
    type: Array,
    default: () => [],
  },
});

// 定义事件
const emit = defineEmits(["delete", "restore"]);

// 删除待办
const deleteTodo = (id) => {
  emit("delete", id);
};

// 恢复待办
const restoreTodo = (id) => {
  emit("restore", id);
};

// 格式化日期
const formatDate = (dateString) => {
  try {
    const date = new Date(dateString);
    const today = new Date();
    const tomorrow = new Date(today);
    tomorrow.setDate(tomorrow.getDate() + 1);

    // 判断是否是今天或明天
    if (date.toDateString() === today.toDateString()) {
      return "今天";
    } else if (date.toDateString() === tomorrow.toDateString()) {
      return "明天";
    } else {
      return `${date.getMonth() + 1}月${date.getDate()}日`;
    }
  } catch (e) {
    return dateString;
  }
};
</script>

<style>
.todo-list {
  width: 100%;
  padding: 20rpx 0;
  box-sizing: border-box;
}

.empty-list {
  padding: 100rpx 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.empty-text {
  font-size: 28rpx;
  color: #999999;
}

.todo-items {
  width: 100%;
}

.todo-item {
  display: flex;
  align-items: center;
  background-color: #ffffff;
  padding: 30rpx;
  margin-bottom: 20rpx;
  border-radius: 12rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  box-sizing: border-box;
  width: 100%;
  opacity: 0.85;
}

.checkbox {
  width: 44rpx;
  height: 44rpx;
  border-radius: 50%;
  border: 2rpx solid #cccccc;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 20rpx;
  flex-shrink: 0;
}

.checkbox.completed {
  background-color: #4361ee;
  border-color: #4361ee;
}

.check-icon {
  width: 32rpx;
  height: 32rpx;
  color: #ffffff;
}

.todo-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0; /* 防止flex子项溢出 */
}

.todo-text {
  font-size: 30rpx;
  color: #333333;
  line-height: 1.5;
}

.completed-text {
  text-decoration: line-through;
  color: #999999;
}

.todo-date {
  font-size: 24rpx;
  color: #999999;
  margin-top: 8rpx;
}

.action-buttons {
  display: flex;
  flex-direction: column;
  gap: 10rpx;
  margin-left: 20rpx;
}

.action-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10rpx;
  border-radius: 50%;
  width: 36rpx;
  height: 36rpx;
  background-color: #f5f5f5;
}

.restore-btn {
  color: #4361ee;
}

.delete-btn {
  color: #ef4444;
}

.action-icon {
  width: 28rpx;
  height: 28rpx;
}
</style>
